<?php

/**
 * This is the model class for table "{{pages}}".
 *
 * The followings are the available columns in table '{{pages}}':
 * @property integer $id
 * @property string $title
 * @property string $alias
 * @property string $text
 * @property integer $created
 * @property string $meta_keys
 * @property string $meta_desc
 */
class Pages extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Pages the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{pages}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, text', 'required'),
            array('alias', 'getAlias'),
            array('created', 'getCreated'),
            array('meta_keys', 'getKeywords'),
            array('meta_desc', 'getDescription'),
			array('title, alias', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, alias, text', 'safe', 'on'=>'search'),
		);
	}
    
    public function getCreated($attribute,$params)
    {
        if (@$this->created) $this->created = date('Y-m-d H:i:s');
    }
    
    public function getAlias($attribute,$params)
    {
        if ($this->id) $addSql = " AND `id` != {$this->id}";        
        $this->alias = SeoHelper::getAliasUrl($this->alias);
        if (trim($this->alias) == '') {
            $this->alias = StringHelper::translit($this->title);
            $this->alias = SeoHelper::getAliasUrl($this->alias);
        }
        $this->alias = SeoHelper::getUniqueAliasDB($this->alias, '{{pages}}', 'alias', $addSql);
    }
    
    public function getKeywords($attribute,$params)
    {
        if (trim($this->meta_keys) == '') 
            $this->meta_keys = SeoHelper::getKeywords($this->text);
    }
    
    public function getDescription($attribute,$params)
    {
        if (trim($this->meta_desc) == '') 
            $this->meta_desc = SeoHelper::getDescription($this->text);
    }
    
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
    
    public function getUrl()
    {
        return Yii::app()->createUrl('/pages/pages/update', array(
            'id'=>$this->id,
        ));
    }
    
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Заголовок',
			'alias' => 'URL-псевдоним',
			'text' => 'Содержимое',
			'created' => 'Создано',
			'meta_keys' => 'Ключевые слова через запятую (значение тега keywords)',
			'meta_desc' => 'Описание (значение тега description)',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('alias',$this->alias,true);
		$criteria->compare('text',$this->text,true);
		$criteria->compare('created',$this->created);
		$criteria->compare('meta_keys',$this->meta_keys,true);
		$criteria->compare('meta_desc',$this->meta_desc,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}